package com.github.chirspan.xaas.minio.web.rest;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.chirspan.xaas.core.rest.RestResult;
import com.github.chirspan.xaas.core.rest.RestResultStatus;
import com.github.chirspan.xaas.minio.config.MinioConfigurationProperties;
import com.github.chirspan.xaas.minio.exception.SCMinioException;
import com.github.chirspan.xaas.minio.service.MinioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author chenpan
 * @date 2020/6/10 12:04
 */
@RestController
@RequestMapping("/oss")
@Slf4j
public class MinioController {

    @Autowired
    private MinioConfigurationProperties configurationProperties;

    @Autowired
    private MinioService minioService;

    @PostMapping("/upload")
    public RestResult upload(@RequestParam("file") MultipartFile file) throws IOException, SCMinioException {
        if (file.isEmpty() || file.getSize() == 0) {
            return new RestResult(RestResultStatus.NULL_FILE);
        }

        String fileName = configurationProperties.getDefaultPath()
                + IdUtil.fastSimpleUUID()
                + StrUtil.DOT
                + FileUtil.extName(file.getOriginalFilename());
        minioService.upload(fileName, file.getInputStream());
        String url = minioService.getObjectUrl(fileName);
        Map<String, String> resultMap = new HashMap<>(4);
        resultMap.put("name", fileName);
        resultMap.put("url", url);
        return RestResult.OK(resultMap, "上传成功");

    }

    @GetMapping("/download")
    public void download(@RequestParam("file") String fileName, HttpServletResponse response, HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        try (InputStream inputStream = minioService.get(fileName)) {
            //解决乱码
            if ( //IE 8 至 IE 10
                    userAgent.toUpperCase().contains("MSIE") ||
                            //IE 11
                            userAgent.contains("Trident/7.0")) {
                fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            } else {
                fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            }
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            IoUtil.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("文件读取异常", e);
        }
    }

    @DeleteMapping
    public RestResult remove(@RequestParam("file") String fileName) throws SCMinioException {
        minioService.remove(fileName);
        return RestResult.OK(fileName, "删除成功");
    }
}
